﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;


namespace MN.Framework
{
    [Serializable]
    public class MnException : Exception
    {
        // Methods
        public MnException()
        {
        }

        public MnException(string message)
            : base(message, new Exception(message))
        {
            this.Args = new object[] { message };
        }

        protected MnException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.MnMessage = (MnMessage)info.GetValue("SgMessage", typeof(MnMessage));
            this.Args = (object[])info.GetValue("Args", typeof(object[]));
        }

        public MnException(string message, params object[] args)
            : base(string.Format(message, args), new Exception(string.Format(message, args)))
        {
            this.MnMessage = FrameworkMessages.ObsoleteException;
            this.Args = new object[] { string.Format(message, args) };
        }

        public MnException(MnMessage message, params object[] args)
        {
            this.MnMessage = message;
            this.Args = args;
        }

        public MnException(string message, Exception innerException, params object[] args)
            : base(string.Format(message, args), innerException)
        {
            this.MnMessage = FrameworkMessages.ObsoleteException;
            this.Args = new object[] { message, args };
        }

        public MnException(MnMessage message, Exception inner, params object[] args)
            : base(message.DefaultValue, inner)
        {
            this.MnMessage = message;
            this.Args = args;
        }

        public string GetDefaultAdjustedMessage()
        {
            if (this.MnMessage == null)
            {
                return (string)this.Args[0];
            }
            return string.Format(this.MnMessage.DefaultValue, this.Args);
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("SgMessage", this.MnMessage);
            info.AddValue("Args", this.Args);
        }

        public string GetTranslatedAdjustedMessage()
        {
            if (this.MnMessage == null)
            {
                return (string)this.Args[0];
            }
            return string.Format(this.MnMessage.DefaultValue, this.Args);
        }

        // Properties
        public object[] Args { get; set; }

        public override string Message
        {
            get
            {
                if (this.MnMessage != null)
                {
                    return string.Format(this.MnMessage.DefaultValue, this.Args);
                }
                return base.Message;
            }
        }

        public MnMessage MnMessage { get; set; }
    }


}
